<?php
// ini_set('memory_limit', '-1');
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class Webclient extends MY_Controller {
	//private $ws_url = 'http://127.0.0.1:2048/webservice/webserver';
	private $ws_url = 'http://opper.com/webservice/webserver';
	public function __construct() {
		parent::__construct ();
		$this->ws_url =  base_url().'webservice/webserver';
	}
	
	public function index() {
		$params = array ();
		if ($this->input->post ( 'submit' )) {
			$result = '';
			$params ['username'] = $this->input->post ( 'username' );
			$params ['passwd'] = $this->input->post ( 'passwd' );
			$params = json_encode ( $params );
			$function = $this->input->post ( 'function' );
			
			$this->load->library ( 'zend' );
			
			$this->zend->load ( 'Zend_Rest_Client' );
			
			$client = new Zend_Rest_Client ( $this->ws_url );
			//print_r($client);
			//echo $function;exit;
			$http = $client->getHttpClient ();
			$http->setConfig ( array (
					'timeout' => 300 
			) );
			
			switch ($function) {
				case 'login':
					$arr = array('username' => $this->input->post( 'username' ),
					'passwd' => $this->input->post( 'passwd' ),
					'email'   => $this->input->post( 'val1' ),
					'password'   => $this->input->post( 'val2' ),
					'facebookid' => $this->input->post( 'val3' )
					);
					$params = json_encode( $arr );
					$result = $client->login ( $params )->post ();
					break;
				case 'getPassword':
					$arr = array('email'   => $this->input->post( 'username' ),
					'password'   => $this->input->post( 'val2' )
					);
					$params = json_encode( $arr );
					$result = $client->getPassword ( $params )->post ();
					break;
				case 'getCategorys' :
					$result = $client->getCategorys ( $params )->post ();
					break;
				case 'register' :
					$arr = array('email'   => $this->input->post( 'val1' ),
							'password'   => $this->input->post( 'val2' )
						);
					$params = json_encode( $arr );
					$result = $client->register ( $params )->post ();
					break;
				case 'updateProfile' :
					$arr = array('username' => $this->input->post( 'username' ),
							'passwd' => $this->input->post( 'passwd' ),
							'id'   => $this->input->post( 'id' ),
							'first_name'   => $this->input->post( 'val1' ),
							'last_name'   => $this->input->post( 'val2' ),
							'insertion' => $this->input->post( 'val3' ),
							'gender' => $this->input->post( 'val4' ),
							'address' => $this->input->post( 'val5' ),
							'house_number' => $this->input->post( 'val6' ),
							'postcode' => $this->input->post( 'val7' ),
							'place' => $this->input->post( 'val8' ),
							'email' => $this->input->post( 'val9' ),
							'phone' => $this->input->post( 'val10' ),
							'birthday' => $this->input->post( 'val11' ),
							'password' => $this->input->post( 'val12' )
					);
					$params = json_encode( $arr );
					$result = $client->updateProfile ( $params )->post ();
					break;
				case 'addMember' :
					$arr = array('username' => $this->input->post( 'username' ),
							'passwd' => $this->input->post( 'passwd' ),
							'first_name'   => $this->input->post( 'val1' ),
							'last_name'   => $this->input->post( 'val2' ),
							'insertion' => $this->input->post( 'val3' ),
							'gender' => $this->input->post( 'val4' ),
							'address' => $this->input->post( 'val5' ),
							'house_number' => $this->input->post( 'val6' ),
							'postcode' => $this->input->post( 'val7' ),
							'place' => $this->input->post( 'val8' ),
							'email' => $this->input->post( 'val9' ),
							'phone' => $this->input->post( 'val10' ),
							'birthday' => $this->input->post( 'val11' )
					);
					$params = json_encode( $arr );
					$result = $client->addMember ( $params )->post ();
					break;
				case 'updateMember' :
					$arr = array('username' => $this->input->post( 'username' ),
							'passwd' => $this->input->post( 'passwd' ),
							'id'   => $this->input->post( 'id' ),
							'first_name'   => $this->input->post( 'val1' ),
							'last_name'   => $this->input->post( 'val2' ),
							'insertion' => $this->input->post( 'val3' ),
							'gender' => $this->input->post( 'val4' ),
							'address' => $this->input->post( 'val5' ),
							'house_number' => $this->input->post( 'val6' ),
							'postcode' => $this->input->post( 'val7' ),
							'place' => $this->input->post( 'val8' ),
							'email' => $this->input->post( 'val9' ),
							'phone' => $this->input->post( 'val10' ),
							'birthday' => $this->input->post( 'val11' )
					);
					$params = json_encode( $arr );
					$result = $client->updateMember ( $params )->post ();
					break;
				case 'getProfile' :
					$arr = array('username' => $this->input->post( 'username' ),
					'passwd' => $this->input->post( 'passwd' ),
					'id'   => $this->input->post( 'id' )
					);
					$params = json_encode( $arr );
					$result = $client->getProfile ( $params )->post ();
					break;
				case 'getListProfile' :
					$arr = array('username' => $this->input->post( 'username' ),
							'passwd' => $this->input->post( 'passwd' )
					);
					$params = json_encode( $arr );
					$result = $client->getListProfile ( $params )->post ();
					break;
				case 'addContract' :
					$arr = array('username' => $this->input->post( 'username' ),
							'passwd' => $this->input->post( 'passwd' ),
							'member_id'   => $this->input->post( 'id' ),
							'contract_number'   => $this->input->post( 'val1' ),
							'category_id'   => $this->input->post( 'val2' ),
							'provider_id' => $this->input->post( 'val3' ),
							'product_id' => $this->input->post( 'val4' ),
							'start_date' => $this->input->post( 'val5' ),
							'end_date' => $this->input->post( 'val6' )
					);
					$params = json_encode( $arr );
					$result = $client->addContract ( $params )->post ();
					break;
				case 'getContract' :
					$arr = array('username' => $this->input->post( 'username' ),
					'passwd' => $this->input->post( 'passwd' ),
					'id'   => $this->input->post( 'id' )
					);
					$params = json_encode( $arr );
					$result = $client->getContract ( $params )->post ();
					break;
				case 'getListContract' :
					$arr = array('username' => $this->input->post( 'username' ),
							'passwd' => $this->input->post( 'passwd' ),
							'member_id'   => $this->input->post( 'id' )
					);
					$params = json_encode( $arr );
					$result = $client->getListContract ( $params )->post ();
					break;
				case 'setTerminationAlert' :
					$arr = array('username' => $this->input->post( 'username' ),
							'passwd' => $this->input->post( 'passwd' ),
							'contract_id'   => $this->input->post( 'id' ),
							'termination_date'   => $this->input->post( 'val1' )
					);
					$params = json_encode( $arr );
					$result = $client->setTerminationAlert ( $params )->post ();
					break;
				case 'setDeath' :
					$arr = array('username' => $this->input->post( 'username' ),
					'passwd' => $this->input->post( 'passwd' ),
					'profile_id'   => $this->input->post( 'id' )
					);
					$params = json_encode( $arr );
					$result = $client->setDeath ( $params )->post ();
					break;
				case 'sendDocument' :
					$arr = array('username' => $this->input->post( 'username' ),
					'passwd' => $this->input->post( 'passwd' ),
					'type'   => $this->input->post( 'id' ),
					'category_id'   => $this->input->post( 'val2' ),
					'provider_id' => $this->input->post( 'val3' ),
					'product_id' => $this->input->post( 'val4' ),
					'member_id'   => $this->input->post( 'val5' ),
					'contract_number'   => $this->input->post( 'val1' )
					);
					$params = json_encode( $arr );
					$result = $client->sendDocument ( $params )->post ();
					break;
				case 'changeAddress' :
					$arr = array('username' => $this->input->post( 'username' ),
							'passwd' => $this->input->post( 'passwd' ),
							'member_id'   => $this->input->post( 'id' ),
							'address'   => $this->input->post( 'val1' ),
							'postcode'   => $this->input->post( 'val2' ),
							'place'   => $this->input->post( 'val3' ),
							'date_move'   => $this->input->post( 'val4' ),
					);
					$params = json_encode( $arr );
					$result = $client->changeAddress ( $params )->post ();
					break;
				case 'addTerminalLetter' :
					$result = $client->addTerminalLetter ( $params )->post ();
					break;
				case 'getListContractHistory' :
					$result = $client->getListContractHistory ( $params )->post ();
					break;
				default :
					// code...
					break;
			}
			$params = json_decode ( $params, true );
			$params ['username'] = $this->input->post ( 'username' );
			$params ['result'] = $result->response ();
		}
		
		$params ['functions'] = array (
				'login' => 'login',
				'getPassword' => 'getPassword',
				'getCategorys' => 'getCategorys',
				'register' => 'register',
				'addMember' => 'addMember',
				'getProfile' => 'getProfile',
				'updateProfile' => 'updateProfile',
				'updateMember' => 'updateMember',
				'getListProfile' => 'getListProfile',
				'addContract' => 'addContract',
				'getListContract' => 'getListContract',
				'getContract' => 'getContract',
				'setTerminationAlert' => 'setTerminationAlert',
				'setDeath' => 'setDeath',
				'sendDocument' => 'sendDocument',
				'changeAddress' => 'changeAddress',
				'addTerminalLetter' => 'addTerminalLetter',
				'getListContractHistory' => 'getListContractHistory' 
		)
		;
		$params ['func'] = $this->input->post ( 'function' );
		$this->load->view ( 'test.php', $params );
	}
	
	// Zip one by one file
	public function zipFile($arraycontents, $pathfolder, $filename) {
		$this->load->library ( 'zip' );
		foreach ( $arraycontents as $url ) {
			$this->zip->read_file ( $url, false, true );
		}
		$pathzip = $pathfolder . "/" . $filename . ".zip";
		$this->zip->archive ( $pathzip );
	}
	function zipFiles($data = array(), $pathfolder, $filename, $keep_last = false) {
		$zip = new ZipArchive ();
		$zip->open ( $pathfolder . $filename, ZIPARCHIVE::OVERWRITE );
		foreach ( $data as $file ) {
			$last_folder = '';
			$paths = explode ( '/', $file );
			if ($keep_last === TRUE) {
				if (count ( $paths ) > 2)
					$last_folder = $paths [count ( $paths ) - 2] . '/';
			}
			$file_name = end ( $paths );
			$zip->addFile ( $file, $last_folder . $file_name );
		}
		$zip->close ();
	}
}
